import copy
from django.utils import simplejson as json
from google.appengine.api import channel
from google.appengine.api import users
from google.appengine.ext import webapp
import model
import common
import observer
import random
import logging
PLANNING_ROOM = "review_room"

class StoryBackLogHandler(common.BaseRequestHandler):
    def _get(self):
        id = self.request.get('id')
        iteration = model.Iteration.get_by_id(int(id))
        if iteration.get_project().is_user_valid():
            user = users.get_current_user()
            channel_id = user.user_id() + '_' + str(random.randint(1, 10000))
            token = channel.create_channel(channel_id)
            observable = observer.Observable(PLANNING_ROOM)
            observable.add_observer({user.nickname(): channel_id})
            self.write_out('stories_backlog.html', {
                          'iteration': iteration,
                          'token': token,
                          'channel_id':channel_id
                          })
        else:
            self.not_allowed_error()
    def _post(self):
        id = self.request.get('id')
        iteration = model.Iteration.get_by_id(int(id))
        if iteration.get_project().is_user_valid():
#            user = users.get_current_user()
#            channel_id = user.user_id() + '_' + str(random.randint(1, 10000))
#            token = channel.create_channel(channel_id)
#            observable = common.Observable(PLANNING_ROOM)
#            observable.add_observer({user.nickname(): channel_id})
#            self.write_out('stories_backlog.html', {
#                          'iteration': iteration,
#                          'token': token,
#                          'channel_id':channel_id
#                          })
            html = self.generate('stories_backlog.html', {
                          'iteration': iteration,
#                          'token': token,
#                          'story_form':form.StoryForm()
                          })
            self.json_response(html)
        else:
            self.not_allowed_error()

class ObserverEventHandler(webapp.RequestHandler):
    def post(self):
        observable = observer.Observable("global")
        observable.notify_observers(self.get_response())
        
class StoryObservationHandler(ObserverEventHandler):
    def get_response(self):
        story_id = self.request.get('story_id')
        text = self.request.get('text')
        story = model.Story.get_by_id(int(story_id))
        justification = model.Justification(story=story,
                                            author=users.get_current_user(),
                                            text=text)
        justification.put()
        response = {'event': 'justification',
            'args': {'story_id': story.key().id(),
            'user': users.get_current_user().nickname(),
            'text': justification.text}}
        return json.dumps(response)

class CommentHandler(ObserverEventHandler):
    def get_response(self):
        story_id = self.request.get('story_id')
        text = self.request.get('comment')
        story = model.Story.get_by_id(int(story_id))
        comment = model.ReviewComment(story=story,
                                author=users.get_current_user(),
                                text=text)
        comment.put()
        response = {'event': 'comment',
                'args': {'story_id':story_id,
                'user': comment.author.nickname(),
                'comment':comment.text}}
        return json.dumps(response)

#class ChatHandler(ObserverEventHandler):
#    def get_response(self):
#        text = self.request.get('message')
#        iteration_id = self.request.get('iteration_id')
#        iteration = model.Iteration.get_by_id(int(iteration_id))
#        message = model.ReviewMessage(iteration=iteration,
#                              user=users.get_current_user(),
#                              text=text)
#        message.put()
#        response = {'event': 'chat',
#            'args': {'message': text,
#            'user': users.get_current_user().nickname()}}
#       return json.dumps(response)


class CopyStories(webapp.RequestHandler):
    def post(self):
        
        des_iteration_id = self.request.get('des_iteration')
        des_iteration = model.Iteration.get(des_iteration_id)
        id_stories = self.request.get_all('story')
        for id_story in id_stories:
            story = model.Story.get_by_id(int(id_story))
            new_story = model.Story(
                story_container = des_iteration,
                name = story.name,
                category = story.category,
                priority = story.priority,
                status = story.status,
                assigned_to = story.assigned_to,
                estimated_time = story.estimated_time,
                description = story.description,
                opened_by = story.opened_by)

            new_story.put()
